package com.example.myproject.web;

import com.example.myproject.redis.redission.service.RedisLockHelper;
import io.swagger.annotations.ApiOperation;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedissionController {
    private Logger logger = LoggerFactory.getLogger(RedissionController.class);

    @Autowired
    private RedisLockHelper redisLockHelper;

    @ApiOperation(value="lock1", notes="")
    @RequestMapping(value="/lock1", method= RequestMethod.GET)
    public void lock1(String uuid){
        logger.info("lock1---");
        RLock rLock =  redisLockHelper.tryLockInTime(uuid,60,80);
        logger.info("lock1---no");
        while (true){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
       }

//        if(rLock!=null){
//            rLock.unlock();
//        }
    }

    //测试2分钟之后等不到是不是返回false了
    //测试发现了trylock 会等待这个时间，
    @ApiOperation(value="lock2", notes="lock2")
    @RequestMapping(value="/lock2", method= RequestMethod.GET)
    public String lock2(String uuid){
        logger.info("lock2start---");
        RLock rLock =  redisLockHelper.tryLockInTime(uuid,80,70);
        logger.info("locke2 end,===="+rLock.isLocked());
        //java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: ae2d3d46-17b4-438e-b273-9bee7ca6cd29 thread-id: 91

        if(rLock!=null){
            rLock.unlock();
        }
        return "ok";
    }

    //测试2分钟之后等不到是不是返回false了
    @ApiOperation(value="forceUnlock", notes="forceUnlock")
    @RequestMapping(value="/forceUnlock", method= RequestMethod.GET)
    public String forceUnlock(String uuid){
        logger.info("forceUnlockStart---");
        redisLockHelper.forceUnlock(uuid);
        logger.info("forceUnlockEnd---");
        return "ok";

    }



    @ApiOperation(value="testReleasTime", notes="测试释放锁的时间，如果停止了，服务器停止了，是不是过期时间是3分钟")
    @RequestMapping(value="/testReleasTime", method= RequestMethod.GET)
    //tryLockInTime realeaseTIme 是key的存活时间
    //这里看门狗这个定时任务会跑吗？60s 20s 延期一次
    //测试发现只要有releaseTime 就不会自动加看门狗，看门狗只有lock的接口才会自动开器锁
    public void testReleasTime(String uuid){
        logger.info("lock1---");
        RLock rLock =  redisLockHelper.tryLockInTime(uuid,60,60);
        while (true){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //测试2分钟之后等不到是不是返回false了
    @ApiOperation(value="getLock", notes="getLock")
    @RequestMapping(value="/getLock", method= RequestMethod.GET)
    public String getLock(String uuid){
        logger.info("getLock1111---");
        RLock rLock =  redisLockHelper.getLockByDataUuid(uuid);

        if(rLock!=null){
            logger.info("getLock,lockflag::"+rLock.isLocked()+",thread:"+rLock.isHeldByCurrentThread());
        }

        logger.info("getLock11112222---");
        return "ok";

    }
}
